package com.wpf.servlet;

import java.io.IOException;
import java.util.ArrayList;
import com.wpf.bean.Pagebean;
import com.wpf.bean.Querybean;
import com.wpf.bean.Userbean;
import com.wpf.dao.UserDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

public class Query extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public Query() {}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Pagebean pagebean = new Pagebean();
		UserDAO userdao = new UserDAO();
		Querybean querybean = new Querybean();
		String findKey =  request.getParameter("findkey");
		int findWay =  Integer.parseInt((String)request.getParameter("findway"));
		String findContent =  request.getParameter("findcontent");
		String findSort = request.getParameter("findsort");
		int pageSize = Integer.parseInt((String)request.getParameter("pagesize"));
		int dbAllRow = 0;//查询返回的总记录数
		int pageCount = 0;//分页后总页数
		ArrayList<Userbean> list = null;
		if(findContent==""||findContent==null) {
			list = userdao.findAll(findKey,findSort);
		}else {
			list = userdao.findbykey(findKey,findWay,findContent,findSort);
		}
		if(list==null) {
			session.setAttribute("operationtip","<font size='2' color='red'>数据库未开启</font>");
		}else if(list.size()==0) {
			session.setAttribute("operationtip","<font size='2' color='red'>系统无数据</font>");
		}else{
			dbAllRow = list.size();
			pageCount = userdao.pageMess(list,dbAllRow,pageSize);
			pagebean.setDbAllRow(dbAllRow);
			pagebean.setPageSize(pageSize);
			pagebean.setPageCount(pageCount);
			pagebean.setRowSet(list);
		}
		session.setAttribute("pageresult", pagebean);
		
		querybean.setFindKey(findKey);
		querybean.setFindway(findWay);
		querybean.setFindsort(findSort);
		querybean.setFindcontent(findContent);
		querybean.setPageSize(pageSize);
		querybean.setLastrowSet(list);
		session.setAttribute("lastresult", querybean);
		session.setAttribute("operationtip", "");
		request.getRequestDispatcher("/wpf_ShowPage").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
